using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.DesignerSupport;

namespace XMTAEditor
{
	public partial class XMTAEditControl : UserControl
	{
		public XMTAEditControl()
		{
			InitializeComponent();
			vsImages.Images.Clear();
			vsImages.TransparentColor = Properties.Resources.VSObject_Class.GetPixel(0, 0);
			vsImages.Images.Add(Properties.Resources.VSObject_Library);
			vsImages.Images.Add(Properties.Resources.VSObject_Namespace);
			vsImages.Images.Add(Properties.Resources.VSObject_Class);
			vsImages.Images.Add(Properties.Resources.VSObject_Event);
			vsImages.Images.Add(Properties.Resources.VSObject_Properties);
			vsImages.Images.Add(Properties.Resources.VSObject_Method);
		}

		private NetCFMetadata metadata;

		public NetCFMetadata Metadata
		{
			get { return metadata; }
			set { metadata = value; ResetTree(); }
		}

		private void ResetTree()
		{
			asmTree.Nodes.Clear();
			SetEditor(null);
			if (metadata == null) return;
			foreach (ClassType ct in metadata.Class)
			{
				TreeNode n = new TreeNode(ct.Name, 2, 2);
				n.Tag = ct;
				if (ct.Event != null)
				{
					foreach (EventType et in ct.Event)
					{
						TreeNode en = new TreeNode(et.Name, 3, 3);
						en.Tag = et;
						n.Nodes.Add(en);
					}
				}
				if (ct.Property != null)
				{
					foreach (PropertyType pt in ct.Property)
					{
						TreeNode pn = new TreeNode(pt.Name, 4, 4);
						pn.Tag = pt;
						n.Nodes.Add(pn);
					}
				}
				if (ct.Method != null)
				{
					foreach (MethodType mt in ct.Method)
					{
						TreeNode mn = new TreeNode(mt.Name, 5, 5);
						mn.Tag = mt;
						n.Nodes.Add(mn);
					}
				}
				asmTree.Nodes.Add(n);
			}
		}

		private string CurrentClass
		{
			get
			{
				if (asmTree.SelectedNode == null)
					return null;
				if (asmTree.SelectedNode.Level == 0)
					return asmTree.SelectedNode.Text;
				return asmTree.SelectedNode.Parent.Text;
			}
		}

		private void asmTree_AfterSelect(object sender, TreeViewEventArgs e)
		{
			BaseEditor ed = (splitContainer1.Panel2.Controls.Count == 1 && splitContainer1.Panel2.Controls[0] is BaseEditor) ? (BaseEditor)splitContainer1.Panel2.Controls[0] : null;
			if (e.Node.Tag is ClassType)
			{
				if (!(ed is ClassEditor))
					SetEditor(ed = new ClassEditor());
				((ClassEditor)ed).Class = (ClassType)e.Node.Tag;
			}
			else if (e.Node.Tag is EventType)
			{
				if (!(ed is EventEditor))
					SetEditor(ed = new EventEditor());
				((EventEditor)ed).Event = (EventType)e.Node.Tag;
			}
			else if (e.Node.Tag is PropertyType)
			{
				if (!(ed is PropertyEditor))
					SetEditor(ed = new PropertyEditor());
				((PropertyEditor)ed).Property = (PropertyType)e.Node.Tag;
			}
			else if (e.Node.Tag is MethodType)
			{
				if (!(ed is MethodEditor))
					SetEditor(ed = new MethodEditor());
				((MethodEditor)ed).Method = (MethodType)e.Node.Tag;
			}
			else
				SetEditor(ed = null);

			if (ed != null)
				ed.ClassName = this.CurrentClass;
		}

		private void SetEditor(BaseEditor ed)
		{
			splitContainer1.Panel2.Controls.Clear();
			if (ed != null)
			{
				ed.Dock = DockStyle.Fill;
				splitContainer1.Panel2.Controls.Add(ed);
			}
		}

		private void asmTree_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (splitContainer1.Panel2.Controls.Count == 1 && splitContainer1.Panel2.Controls[0] is BaseEditor)
				((BaseEditor)splitContainer1.Panel2.Controls[0]).Save();
		}

		public void Save(string filename)
		{
			MetadataFactory.Save(this.metadata, filename);
		}
	}
}
